'use strict';
const fs = require('fs');
const path = require('path');
const Controller = require('egg').Controller;

class CommiteController extends Controller {
    async reply() {
        const { ctx } = this;
        let res = { "code": 0, "msg": "成功" };
        const reply = ctx.request.query;
        // reply.cid = ctx.session.cid;
        console.log(11, reply);
        reply.content = encodeURI(reply.content);
        const result = await this.app.mysql.insert('comments', reply); // 在 post 表中，插入 title 为 Hello World 的记录

        //   select addtimes,content from comments where cid = 2;
        //  INSERT INTO `posts`(`title`) VALUES('Hello World');
        // console.log(r);
        // res = r;
        ctx.body = reply;

    }
    async upload() {
        const { ctx } = this;
        const data = {
            "errno": 0,
            "data": {
                "url": "xxx",
                "alt": "yyy",
                "href": "zzz"
            }
        }
        // let res = { "code": 0, "msg": "成功" };
        // const upload = ctx.request.query;
        const file = ctx.request.files[0];
        // let sql1 = `INSERT INTO comments (uid,aid,content) VALUE (${upload.uid},${upload.aid},"${upload.data}");`;
        // let f = await this.app.mysql.query(sql1);
        fs.copyFileSync(file.filepath, path.dirname(__dirname) + '/public/uploads/' + path.basename(file.filepath));
        // data.data.url = '/api/public/uploads/' + path.basename(file.filepath);
        data.data.url = 'http://127.0.0.1:7001/public/uploads/' + path.basename(file.filepath);
        data.data.alt = file.filename;
        console.log(file);
        ctx.body = data;

    }
}

module.exports = CommiteController;
